﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace DnPakExplorer.Forms {
	public partial class TestLayeredWindow : Form {

		Sprite mBacksprit = new Sprite(Properties.Resources.dialogframe072, 5, 5, 5, 5);

		public TestLayeredWindow() {
			InitializeComponent();
			Repaint();
		}

		protected override CreateParams CreateParams {
			get {
				CreateParams cp  =   base.CreateParams;
				cp.ExStyle  |=   0x00080000;  //  WS_EX_LAYERED 扩展样式
				return cp;
			}
		}

		public void Repaint() {
			IntPtr screenDc  =  Win32.GetDC(IntPtr.Zero);
			

			IntPtr memDc  =  Win32.CreateCompatibleDC(screenDc);
			IntPtr hBitmap  =  IntPtr.Zero;
			IntPtr oldBitmap  =  IntPtr.Zero;
			
			try {
				//Bitmap bmp = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); 

				//hBitmap  =  bitmap.GetHbitmap(Color.FromArgb(0));   // 创建GDI位图句柄，效率较低
				//hBitmap = bmp.GetHbitmap(Color.FromArgb(0));
				hBitmap = Win32.CreateBitmap(Width, Height, 1, 32, IntPtr.Zero);

				oldBitmap  =  Win32.SelectObject(memDc, hBitmap);
				Win32.Size size  =   new Win32.Size(Width, Height);
				Win32.Point pointSource  =   new Win32.Point(0, 0);
				Win32.Point topPos  =   new Win32.Point(Left, Top);
				Win32.BLENDFUNCTION blend  =   new Win32.BLENDFUNCTION();
				blend.BlendOp              =  Win32.AC_SRC_OVER;
				blend.BlendFlags           =   0;
				blend.SourceConstantAlpha  =  100;
				blend.AlphaFormat          =  Win32.AC_SRC_ALPHA;

				Graphics g = Graphics.FromHdc(memDc);
				mBacksprit.Draw(g, new Rectangle(0, 0, Width, Height));
				
				
				Win32.UpdateLayeredWindow(Handle, screenDc, ref  topPos, ref  size, memDc, ref  pointSource, 0, ref  blend, Win32.ULW_ALPHA);
			} finally {
				Win32.ReleaseDC(IntPtr.Zero, screenDc);
				if (hBitmap  !=  IntPtr.Zero) {
					Win32.SelectObject(memDc, oldBitmap);

					Win32.DeleteObject(hBitmap);
				}
				Win32.DeleteDC(memDc);
			}
		}



		

		private void TestLayeredWindow_SizeChanged(object sender, EventArgs e) {
			Repaint();
		}

		private void TestLayeredWindow_Paint(object sender, PaintEventArgs e) {

		}
	}

}
